<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/jquery-1.6.2.min.js"></script>


<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/crypto.js"></script>
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/sha1.js"></script>
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/hmac.js"></script>
<script type="text/javascript" language="javascript"
	src="<%=basePath%>JS/encode/md5.js"></script>

<script type="text/javascript">



function clone(myObj)
{
    if(typeof(myObj) != 'object') return myObj;
    if(myObj == null) return myObj;
    var myNewObj = new Object(); 
    for(var i in myObj) myNewObj[i] = clone(myObj[i]); 
    return myNewObj;
}

	$(function() {

		var dateTime = new Date();
		var timeStap = function(){

			return dateTime.getYear()+ "" + dateTime.getMinutes() + dateTime.getSeconds();
		};
		var params = {
	
				
				//"oauth_callback" : encodeURIComponent("<%=basePath%>index.jsp"),
				"oauth_consumer_key" : "3607363939",
				"oauth_nonce" :String(new Date().getTime()),
				"oauth_signature_method" : "HMAC-SHA1",
				//"oauth_token_secret": "65d07369655bb4dfd05946d99340b9ec",
				"oauth_timestamp" : String(new Date().getTime()).substring(0,10),
				"oauth_version" :  "1.0"
				
	
		};

	
	var httpMethod = encodeURIComponent("http://api.t.sina.com.cn/oauth/request_token");
	var props = "";
		function allPrpos(obj) { 
		    // 用来保存所有的属性名称和值
		  
		    // 开始遍历
		    for(var p in obj){ 
		        // 方法
		        if(typeof(obj[p])=="function"){ 
		            obj[p]();
		        }else{ 
		            // p 为属性名称，obj[p]为对应属性的值
		            props+= p + "%3D" + obj[p] +"%26";
		        } 
		    } 

		    props = props.substring(0,props.length-3)
		    
		    // 最后显示所有的属性
		   // alert(props);
		}
		allPrpos(params);


		$("#props").html(props);
		var newParams = clone(params);

	

			
		var message = "GET&" + httpMethod + "&" + props;

		$("#message").html(message);
		//alert(message);
		var secret = "65d07369655bb4dfd05946d99340b9ec&";

		var bytes = Crypto.HMAC(Crypto.SHA1, message, secret, { asBytes: true }) ;
		//但仅仅这样还不行, 还得转换成Base64.
		//在Crypto.util下找到相应的方法.
		var signature = Crypto.util.bytesToBase64(bytes).replace(/=/,"%3D");

		$("#signature").html(signature);

		//打印出时间 
		//$("#time").html( String(new Date().getTime()).substring(0,10));
		

		newParams["oauth_signature"] = signature;


		$("#share").click(
				function() {
					$.ajax( {
						type : "get",
						url : "http://api.t.sina.com.cn/oauth/request_token",
						data : newParams,
						success : function(_result) {
							alert(_result);
						},
						headers: {
				            "Content-Type": ""
				        },
						error : function() {
							alert("无数据返回 ！");
						}
					});
	
				});


		//显示属性
		$("#xianshishuxing").click(function(){

			getObjpro(newParams);

		});
	
		

		

	});

	//下面方法多余 ，只用于调用属性的显示
//==============================================================================================================
function getObjpro(obj) { 
    // 用来保存所有的属性名称和值
  	var pro = "";
    // 开始遍历
    for(var pr in obj){ 
        // 方法
        if(typeof(obj[pr])=="function"){ 
            obj[pr]();
        }else{ 
            // p 为属性名称，obj[p]为对应属性的值
            pro += pr + "=" + obj[pr] +"<br/>";
        } 
    } 

	$("#qingqiu").html(pro);
    
}
		

	
</script>
<title>共享</title>
</head>
<body>


<button id="share">响应</button>
<button id="xianshishuxing">显示属性</button>

<h2>message</h2>
<hr>
<div id="message"></div>

<h2>signature</h2>
<hr>
<div id="signature"></div>
<h2>time</h2>
<hr>
<div id="time"></div>

<h2>props</h2>
<hr>
<div id="props"></div>

<h2>请求</h2>
<hr>
<div id="qingqiu"></div>

</body>
</html>